<template>
<div class="sub-sort">
    <div class="sort">
        <a :class="{ active: sortParams.sortFieid === null }" @click="changeSort(null)" href="javascript:">
            默认排序
        </a>
        <a :class="{ active: sortParams.sortFieid === 'publishTime' }" @click="changeSort('publishTime')" href="javascript:">
            最新商品</a>
        <a :class="{ active: sortParams.sortFieid === 'orderNum' }" @click="changeSort('orderNum')" href="javascript:">最高人气
        </a>
        <a :class="{ active: sortParams.sortFieid === 'evaluateNum' }" @click="changeSort('evaluateNum')" href="javascript:">
            评论最多
        </a>
        <a href="javascript:">
            价格排序
            <i :class="{ active: sortParams.sortFieid === 'price' && sortParams.sortMethod == 'asc' }" class="arrow up"></i>
            <i :class="{ active: sortParams.sortFieid === 'price' && sortParams.sortMethod == 'desc' }" class="arrow down"></i>
        </a>
    </div>
    <div class="check">
        <XtxCheckbox @change="changeCheck" v-model="sortParams.inventory">仅显示有货商品</XtxCheckbox>
        <XtxCheckbox @change="changeCheck" v-model="sortParams.onlyDiscount">仅显示特惠商品</XtxCheckbox>
    </div>
</div>
</template>

<script setup>
import { ref } from 'vue'
import XtxCheckbox from "@/components/library/xtx-checkbox.vue";

// 1. 跟后台需要的参数定义数据对象
// 2. 根据数据对象，绑定组件（复选框，排序组件）
// 3. 在操作排序组件的时候，需要反馈给数据对象
// sortFIELD ===> publishTime,orderNum,price,evaluateNum
// sortMethod ===> asc为正序 desc 为倒序

const sortParams = ref({
    inventory: false,
    onlyDiscount: false,
    sortFieid: null,
    sortMethod: null
})

const emit = defineEmits(['sort-change'])
// 改变排序
const changeSort = (sortFieid) => {
    if (sortFieid === 'price') {
        sortParams.value.sortFieid = sortFieid
        if (sortParams.value.sortMethod === null) {
            // 第一次点击，默认是降序
            sortParams.value.sortMethod = 'desc'
        } else {
            // 其他情况根据当前排序取反
            sortParams.value.sortMethod = sortParams.value.sortMethod === 'desc' ? 'asc' : 'desc'
        }
    } else {
        // 如果排序未改变定制逻辑
        if (sortParams.value.sortFieid === sortFieid) return
        sortParams.value.sortFieid = sortFieid
        sortParams.value.sortMethod = null
    }
    // 触发 sort-change 事件
    emit('sort-change', sortParams)
}

const changeCheck = (sortFieid) => {
    emit('sort-change', sortParams)
}
</script>

<style scoped lang="less">
@import "@/assets/styles/variables";

.sub-sort {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .sort {
    display: flex;
    a {
      height: 30px;
      line-height: 28px;
      border: 1px solid #e4e4e4;
      padding: 0 20px;
      margin-right: 20px;
      color: #999;
      border-radius: 2px;
      position: relative;
      transition: all .3s;
      &.active {
        background: @xtxColor;
        border-color: @xtxColor;
        color: #fff;
      }
      .arrow {
        position: absolute;
        border: 5px solid transparent;
        right: 8px;
        &.up {
          top: 3px;
          border-bottom-color: #bbb;
          &.active {
            border-bottom-color: @xtxColor;
          }
        }
        &.down {
          top: 15px;
          border-top-color: #bbb;
          &.active {
            border-top-color: @xtxColor;
          }
        }
      }
    }
  }
  .check {
    .xtx-checkbox {
      margin-left: 20px;
      color: #999;
    }
  }
}
</style>